var uniqueItems = function (data, key) {
    var result = [];

    for (var i = 0; i < data.length; i++) {
        var value = data[i][key];

        if (result.indexOf(value) == -1) {
            result.push(value);
        }

    }
    return result;
};


hfrApp.registerCtrl("SitemapManagementController", function ($scope, $rootScope, $http, $window, $translate, $timeout, HfrConfig, HfrStorage, HfrInit) {
	/*
	 * ---------------------- Init Controller ---------------------------
	 */
	//Init message alert
	HfrInit.message();
	
	//Display meta header
	HfrConfig.displayMeta("siteManagement");
	
	//Delay content for display after 0,8 second
	$timeout(function () {
		$scope.showViewContent = true;
	}, 800);
	
	$scope.showFilter = false;
	$scope.showSelector = {};
	$scope.showSelectorContent = {};
	$scope.showReloadSelector = {};
	$scope.totalSelector = {};
	$scope.selectors = {};
	$scope.checkTopic = {};
	$scope.showBlogs = [];
	
	//Get all topic
	$scope.getAllTopics = function () {
		var topicUrl = HfrConfig.ApiServiceUrl.topicsUrlApi + "?customerId=" + $rootScope.username;
		
		$http.get(topicUrl)
			.success(function (data) {
				if (data.status == 200) {
					$scope.topics = data.response.topics;
					//Set default checked
					angular.forEach($scope.topics, function(topic, key) {
						$scope.checkTopic[topic.id] = true;
			    	});
				} else {
					$scope.topics = {};
					$rootScope.reload.display = true;
					$rootScope.reload.text = data.message;
				}
			})
			.error(function (data) {
				$scope.topics = {};
				$rootScope.reload.display = true;
				$rootScope.reload.text = $translate.instant("message.error.connect");
			});
	};
	
	$scope.getAllTopics();
	
	
	
	//Get all blog by customer
	$scope.getAllBlogs = function () {
		var blogUrl = HfrConfig.ApiServiceUrl.blogsUrlApi + "?customerId=" + $rootScope.username;

		$http.get(blogUrl)
			.success(function (data) {
				$scope.showSiteManagement = true;
				if (data.status == 200) {
					$scope.blogs = data.response.blogs;
					$scope.totalBlog = $scope.sizeOf($scope.blogs); 
					//Init filter blog
					$scope.initFilter();
					
					//Selected blog
					if ($rootScope.blogSelected !== undefined) {
						$scope.viewSitemap($rootScope.blogSelected);
						delete $rootScope.blogSelected;
					}
					
				} else {
					$scope.blogs = {};
					$scope.showBlogs = [];
					$scope.totalBlog = 0;
					$rootScope.message.error = true;
					$rootScope.message.text = data.message;
				}
				
			})
			.error(function (data) {
				$scope.blogs = {};
				$scope.showBlogs = [];
				$scope.totalBlog = 0;
				$scope.showSiteManagement = true;
				$rootScope.reload.display = true;
				$rootScope.reload.text = $translate.instant("message.error.connect");
			});
	};
	
	$scope.getAllBlogs();
	
	/*
	 * ---------------------- Process Controller ---------------------------
	 */
	
	$scope.viewSitemap = function (blogId) {
		if ($scope.showSelector[blogId]) {
			$scope.showSelector[blogId] = false;
		} else {
			if ($scope.showSelector[blogId] == undefined) {
				$scope.showSelector[blogId] = true;
				//Get selector
				$scope.getSelector(blogId);
			} else {
				$scope.showSelector[blogId] = true;
			}
			
		}
		
		return false;
	};

	//Edit infomation
	$scope.editBlog = function (blogId, type) {
		$rootScope.blogId = blogId;
		HfrStorage.set("blogId", blogId);
		
		if (type == "Wordpress") {
			$window.location.href= "#/edit-site-wordpress";
		} else {
			$window.location.href= "#/edit-site-blogspot";
		}
		
		return false;
	}
	
	$scope.deleteBlog = function (blogId) {
		var answer = confirm($translate.instant("static.siteManagement.questionDeleteBlog") + "\"" + blogId);
	    if (answer) {
	    	var deleteBlogUrl = HfrConfig.ApiServiceUrl.blogsUrlApi + "/" + encodeURI(blogId);
	    	$http.delete(deleteBlogUrl)
	    		.success(function (data) {
	    			if (data.status == 200) {
	    				$scope.getAllBlogs();
	    				$rootScope.message.success = true;
						$rootScope.message.text = $translate.instant("message.siteManagement.deleteSuccessBlog") + " " + blogId;
	    			} else {
	    				$rootScope.message.error = true;
						$rootScope.message.text = data.message;
	    			}
	    		})
	    		.error(function (data) {
					$rootScope.message.error = true;
					$rootScope.message.text = $translate.instant("message.error.connect");
				});
	    }
	    
	    return false;
	};
	
	//Change Topic
	$scope.changeTopic = function (blogId) {
		$rootScope.siteurl = blogId;
		HfrStorage.set("siteUrl", blogId);
		
		$window.location.href= "#/edit-topics";
		
		return false;
	}
	
	$scope.getSelector = function (blogId) {
		//Set disable reload content
		$scope.showReloadSelector[blogId] = {};

		//Get selectors
		var selectorUrl = HfrConfig.ApiServiceUrl.blogsUrlApi + "/" + encodeURI(blogId) + "/selectors";
		$http.get(selectorUrl)
			.success(function(data) {
				if (data.status == 200) {
					$scope.showSelectorContent[blogId] = true;
					$scope.selectors[blogId] = data.response.selectors;
					$scope.totalSelector[blogId] = $scope.sizeOf($scope.selectors[blogId]);
				} else {
					$scope.totalSelector[blogId] = 0;
					$scope.showReloadSelector[blogId] = {
						display	: true,
						text	: data.message
					};
				}
			})
			.error(function (data) {
				$scope.totalSelector[blogId] = 0;
				
				$scope.showReloadSelector[blogId] = {
					display	: true,
					text	: $translate.instant("message.error.connect")
				};
			});
	};
	
	//Event Add Sitemap
	$scope.addSitemap = function (blogId) {
		delete $rootScope.selectorId;
		$rootScope.blogId = blogId;
		
		HfrStorage.remove("selectorId");
		HfrStorage.set("blogId", blogId);
		
		$window.location.href= "#/add-sitemap";
		
		return false;
	};
	
	//Event Edit Sitemap
	$scope.editSitemap = function (id, blogId) {
		$rootScope.selectorId = id;
		$rootScope.blogId = blogId;
		
		HfrStorage.set("selectorId", id);
		HfrStorage.set("blogId", blogId);
		
		$window.location.href= "#/edit-sitemap";
		
		return false;
	};
	
	//Event delete sitemap
	$scope.deleteSitemap = function (selectorId, blogId, sitemapId) {
		
		var answer = confirm($translate.instant("static.siteManagement.questionDeleteSitemap") + " \"" + sitemapId + "\"?");
	    if (answer) {
	    	var paramUrl = "?dataJson={selectors: [{selectorId: \"" + selectorId + "\", blogId:\"" + blogId + "\"}]}";
			var sitemapBlogUrl = HfrConfig.ApiServiceUrl.selectorsUrlApi + "/blogs" + paramUrl;
			
			$http({method: 'DELETE', url: sitemapBlogUrl})
				.success(function(data) {
					if (data.status == 200) {
						//Get again sitemaps
						$scope.getSelector(blogId);
						$rootScope.message.success = true;
						$rootScope.message.text = $translate.instant("message.siteManagement.deleteSuccessSitemap") + " " + sitemapId;
					} else {
						$rootScope.message.error = true;
						$rootScope.message.text = data.message;
					}
				})
				.error(function (data) {
					$rootScope.message.error = true;
					$rootScope.message.text = $translate.instant("message.error.connect");
				});
	    }
		
		return false;
	};
	
	$scope.initFilter = function () {
		$scope.$watch(function () {
	        return {
	        	checkTopic: $scope.checkTopic,
	        	siteType: $scope.siteType
	        }
	    }, function () {
	    	$scope.showBlogs = [];
	    	angular.forEach($scope.blogs, function(blog, key) {
	    		var display = false; 
	    		if ($scope.siteType == undefined || $scope.siteType == "All") {
	    			display = true;
	    		} else if ($scope.siteType == blog.type) {
					display = true;
				}
	    		
	    		if (display) {
	    			for (var j in $scope.checkTopic) {
			    		display = false;
			    		//Kiem tra xem blog co thuoc topic day khong
			    		if (!display && $scope.checkTopic[j] && blog.topics && blog.topics[j] != undefined) {
			    			display = true;
			    			break;
			    		}
			    	}
	    		}
		    	
		    	if (display) {
	    			$scope.showBlogs.push(blog);
	    		}
	    	});
	    }, true);
	};
	
	
	
	//Count size
	$scope.sizeOf = function(obj) {
	    return Object.keys(obj).length;
	};
});
